<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="编号" width="80" property="restaurantId"/>
      <el-table-column label="店铺名称" align="center" property="restaurantName"/>
      <el-table-column label="地址" align="center">
        <template slot-scope="scope">
          {{ scope.row.provinceAndCity }} {{ scope.row.detailAddress }}
        </template>
      </el-table-column>
      <el-table-column label="联系人信息" align="center">
        <template slot-scope="scope">
          {{ scope.row.restaurantContacts }} {{ scope.row.restaurantTelephone }}
        </template>
      </el-table-column>
      <el-table-column label="图片信息" align="center" width="120">
        <template slot-scope="scope">
          <el-popover placement="bottom" width="850" trigger="click">
            <el-table :data="[1]">
              <el-table-column label="店铺门头照" align="center">
                <template>
                  <el-image
                    style="width: 100px; height: 100px"
                    :src="scope.row.restaurantPhoto"
                    :preview-src-list="[scope.row.restaurantPhoto]"
                  />
                </template>
              </el-table-column>
              <el-table-column label="营业执照" align="center">
                <template>
                  <el-image
                    style="width: 100px; height: 100px"
                    :src="scope.row.restaurantLicense"
                    :preview-src-list="[scope.row.restaurantLicense]"
                  />
                </template>
              </el-table-column>
            </el-table>
            <el-button slot="reference" size="mini">查看</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column align="center" label="申请时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="基本操作" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="confirm(scope.row)">通过</el-button>
          <el-button type="danger" size="mini" @click="cancel(scope.row)">拒绝</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="app-pagination">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handlePrevChange"
        @next-click="handleNextChange"
      />
    </div>
  </div>
</template>

<script>
import { getUserAuthList, confirmUserAuth, cancelUserAuth } from '@/api/user'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success',
        0: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      searchFrom: {
        name: ''
      },
      list: null,
      listLoading: true,
      total: 0,
      limit: 10,
      page: 1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const searchParams = this.searchFrom
      searchParams['page'] = this.page
      searchParams['limit'] = this.limit
      getUserAuthList(searchParams).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    onSearch() {
      this.fetchData()
    },
    /* 上一页*/
    handlePrevChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 下一页*/
    handleNextChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 数字点击*/
    handleCurrentChange(event) {
      this.page = event
      this.fetchData()
    },
    handleSizeChange(event) {
      this.limit = event
      this.fetchData()
    },
    confirm(event) {
      this.$confirm('此操作将该店铺通过审核, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        confirmUserAuth({ restaurantId: event.restaurantId }).then(res => {
          this.$message({
            type: 'success',
            message: '通过成功'
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    },
    cancel(event) {
      this.$confirm('此操作将该店铺拒绝审核, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        cancelUserAuth({ restaurantId: event.restaurantId }).then(res => {
          this.$message({
            type: 'success',
            message: '拒绝成功'
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>
